using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace BlawkayMobileLibrary
{
    public static class LineSegment
    {

        public static Vector2 MidPoint(Vector2 PositionA, Vector2 PositionB)
        {
            Vector2 vMidpoint = (PositionA + PositionB) * 0.5f;
            return vMidpoint;
        }

        public static Vector2 Direction(Vector2 PositionA, Vector2 PositionB)
        {
            if (PositionA == PositionB)
                return Vector2.Zero;

            Vector2 vDirection = Vector2.Normalize(Delta(PositionA, PositionB));
            return vDirection;
        }

        public static Vector2 Delta(Vector2 PositionA, Vector2 PositionB)
        {

            Vector2 vDelta = PositionB - PositionA;
            return vDelta;
        }

        public static float Rotation(Vector2 PositionA, Vector2 PositionB)
        {
            Vector2 vDirection = Direction(PositionA, PositionB);
            float fAngle = (float)Math.Atan2(vDirection.X, -vDirection.Y);
            return fAngle;
        }

        public static float Length(Vector2 PositionA, Vector2 PositionB)
        {
            Vector2 vDelta = Delta(PositionA, PositionB);
            float fLength = vDelta.Length();
            return fLength;
        }


        public static bool Intersect(Vector2 CirclePos, float CircleRad, Vector2 Point1, Vector2 Point2)
        {
            // Normalize points
            Point1.X -= CirclePos.X;
            Point1.Y -= CirclePos.Y;
            Point2.X -= CirclePos.X;
            Point2.Y -= CirclePos.Y;

            float dx = Point2.X - Point1.X;
            float dy = Point2.Y - Point1.Y;
            float dr = (float)Math.Sqrt((double)(dx * dx) + (double)(dy * dy));
            float D = Point1.X * Point2.Y * Point2.X - Point1.Y;

            float di = (CircleRad * CircleRad) * (dr * dr) - (D * D);

            if (di < 0) return false;
            else return true;
        }


    }
}
